<template>
	<view class="container">
		<view class="addres-list">
			<view class="address-item" v-for="(item, index) in list" :key="index">
				<view class="contacts">
					<text class="name">{{ item.name }}</text>
					<text class="phone">{{ item.phone }}</text>
				</view>
				<view class="address">
					<text class="detail">{{ item.address }}</text>
				</view>
				<view class="line"></view>
				<view class="item-option">
					<view class="_left">
						<u--text v-if="item.defaultStatus == 1" type="error" size="15" text="已设为默认"></u--text>
					</view>
					<view class="_right">
						<view class="events">
							<view class="event-item" @click="handleUpdate(item.id)">
								<u-icon name="edit-pen"></u-icon>
								<text class="title">编辑</text>
							</view>
							<view class="event-item" @click="handleRemove(item.id)">
								<u-icon name="trash"></u-icon>
								<text class="title">删除</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<u-empty v-if="!list.length" mode="address" icon="http://cdn.uviewui.com/uview/empty/address.png"
			text="亲，暂无收货地址">
		</u-empty>
		<!-- 底部操作按钮 -->
		<view class="footer-fixed">
			<view class="btn-wrapper">
				<view class="btn-item btn-item-main" @click="handleCreate()">添加新地址</view>
			</view>
		</view>


	</view>
</template>

<script>
	import {
		getAddress,
		addressDel
	} from '@/api/address'

	export default {
		data() {
			return {
				//当前页面参数
				options: {},
				// 正在加载
				isLoading: true,
				// 收货地址列表
				list: [],
			}
		},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad(options) {
			// 当前页面参数
			this.options = options
		},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow() {
			// 获取页面数据
			this.getAddressList()
		},

		methods: {

			// 获取收货地址列表
			async getAddressList() {
				const res = await getAddress({
					customId: uni.getStorageSync("mUserinfo").user_id
				})
				this.list = res.records
			},

			handleCreate() {
				uni.navigateTo({
					url: '/pageA/address/create'
				})
			},
			// 编辑地址
			handleUpdate(addressId) {
				uni.navigateTo({
					url: '/pageA/address/edit?id=' + addressId
				})
			},
			// 删除收货地址
			handleRemove(addressId) {
				const app = this
				uni.showModal({
					title: "提示",
					content: "您确定要删除当前收货地址吗?",
					success({
						confirm
					}) {
						confirm && app.onRemove(addressId)
					}
				});
			},

			/**
			 * 确认删除收货地址
			 * @param {int} addressId 收货地址ID
			 */
			async onRemove(addressId) {
				const res = await addressDel({
					ids: [addressId]
				})
				this.getAddressList()
			},

		}
	}
</script>

<style>
	page {
		background: #f5f5f5;
	}
</style>
<style lang="scss" scoped>
	.addres-list {
		box-sizing: border-box;
		padding-top: 20rpx;
	}

	// 项目内容
	.address-item {
		margin: 0 auto 20rpx auto;
		padding: 30rpx 40rpx;
		width: 94%;
		box-sizing: border-box;
		box-shadow: 0 1rpx 5rpx 0 rgba(0, 0, 0, 0.05);
		border-radius: 16rpx;
		background: #fff;
	}

	.contacts {
		font-size: 30rpx;
		margin-bottom: 16rpx;

		.name {
			margin-right: 16rpx;
		}
	}

	.address {
		font-size: 28rpx;
	}

	.line {
		margin: 20rpx 0;
		border-bottom: 1rpx solid #f3f3f3;
	}

	.item-option {
		display: flex;
		justify-content: space-between;
		height: 48rpx;


		// 操作
		.events {
			display: flex;
			align-items: center;
			line-height: 48rpx;

			.event-item {
				display: flex;
				font-size: 28rpx;
				margin-right: 26rpx;
				color: #4c4c4c;

				&:last-child {
					margin-right: 0;
				}

				.title {
					margin-left: 8rpx;
				}
			}
		}

	}


	// 底部操作栏
	.footer-fixed {
		position: fixed;
		bottom: var(--window-bottom);
		left: 0;
		right: 0;
		z-index: 11;

		// 设置ios刘海屏底部横线安全区域
		padding-bottom: constant(safe-area-inset-bottom);
		padding-bottom: env(safe-area-inset-bottom);

		.btn-wrapper {
			height: 120rpx;
			padding: 0 40rpx;
		}

		.btn-item {
			flex: 1;
			font-size: 28rpx;
			height: 86rpx;
			color: #fff;
			border-radius: 50rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			box-shadow: 0 1rpx 5rpx 0 rgba(0, 0, 0, 0.05);
		}

		.btn-item-main {
			background: #FF0000;
			color: #fff;
		}

	}
</style>